package com.main.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.dao.AwardDAO;
import com.main.dao.TrollDAO;
import com.main.pojo.Response;
import com.main.pojo.TrollJoke;
import com.main.pojo.TrollPhoto;
import com.main.pojo.Uploader;
import com.main.utils.CommonUtils;
import com.main.utils.Constant;

@Controller
public class TrollController {

	private static final Logger logger = LoggerFactory.getLogger(TrollController.class);

	@Autowired
	private TrollDAO trollDao;

	public TrollDAO getTrollDao() {
		return trollDao;
	}

	public void setTrollDao(TrollDAO trollDao) {
		this.trollDao = trollDao;
	}

	@Autowired
	private AwardDAO awardDao;
	
	public AwardDAO getAwardDao() {
		return awardDao;
	}

	public void setAwardDao(AwardDAO awardDao) {
		this.awardDao = awardDao;
	}
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String trollHome(Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollPhoto> trollList = trollDao.getTrollPhoto(1);
			String msg = trollDao.getMessage();
			model.addAttribute("trollList", trollList);
			model.addAttribute("message", msg);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "index";
	}
	
	@RequestMapping(value = "/new/{page}", method = RequestMethod.GET)
	public String trollHome(@PathVariable int page,Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollPhoto> trollList = trollDao.getTrollPhoto(((page-1)*6/Constant.PHOTO_PAGE_SIZE) + 1);
			String msg = trollDao.getMessage();
			model.addAttribute("trollList", trollList);
			model.addAttribute("message", msg);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "index";
	}

	@RequestMapping(value = "/loadTroll", method = RequestMethod.POST)
	public String loadTrolls(@RequestParam(value = "loadedSize", required = false) int loadedSize, Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollPhoto> trollList = new ArrayList<TrollPhoto>();
			if (loadedSize >= 0) {
				trollList = trollDao.getTrollPhoto((loadedSize / Constant.PHOTO_PAGE_SIZE) + 1);
			}
			model.addAttribute("trollList", trollList);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "photosContent";
	}

	@RequestMapping(value = "/loadTrollByUploader", method = RequestMethod.POST)
	public String loadTrolls(@RequestParam(value = "loadedSize", required = false) int loadedSize,
			@RequestParam(value = "uploaderId", required = false) int uploaderId, Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollPhoto> trollList = new ArrayList<TrollPhoto>();
			if (loadedSize >= 0) {
				int page = (loadedSize / Constant.PHOTO_PAGE_SIZE) + 1;
				trollList = trollDao.getTrollPhotoByUploader(uploaderId, page);
			}
			model.addAttribute("trollList", trollList);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "photosContent";
	}

	@RequestMapping(value = "/photo/{trollId}", method = RequestMethod.GET)
	public String trollDetail(@PathVariable int trollId, HttpServletRequest request, Model model) {
		String path = "";
		try {
			TrollPhoto trollNews = trollDao.getTrollPhotoById(trollId);
			Uploader uploader = (Uploader) request.getSession().getAttribute("uploader");
			if (trollNews != null) {
				if (trollNews.getStatus().equals(Constant.APPROVED)) {
					model.addAttribute("trollNews", trollNews);
				} else {
					if (trollNews.getStatus().equals(Constant.PENDING)) {
						if (uploader != null && trollNews.getUploader().getId().equals(uploader.getId())) {
							model.addAttribute("trollNews", trollNews);
							model.addAttribute("message",
									"Ảnh đã upload thành công. Nó sẽ được hiển thị ở trang chủ sau khi được duyệt !!!");
						}
					}
				}
			}
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "photoDetail";
	}

	@RequestMapping(value = "/truyen-cuoi", method = RequestMethod.GET)
	public String getTrollJokes(Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollJoke> jokeList = trollDao.getTrollJoke(1);
			model.addAttribute("jokeList", jokeList);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "joke";
	}

	@RequestMapping(value = "/loadJoke", method = RequestMethod.POST)
	public String loadJokes(@RequestParam(value = "loadedSize", required = false) int loadedSize, Model model, HttpServletRequest request) {
		String path = "";
		try {
			List<TrollJoke> jokeList = new ArrayList<TrollJoke>();
			if (loadedSize >= 0) {
				jokeList = trollDao.getTrollJoke((loadedSize / Constant.JOKE_PAGE_SIZE) + 1);
			}
			model.addAttribute("jokeList", jokeList);
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "jokesContent";
	}
	
	@RequestMapping(value = "/load-joke-relative", method = RequestMethod.GET)
	public @ResponseBody List<TrollJoke> loadJokeRelative(@RequestParam(value = "page") int page, Model model) {
		List<TrollJoke> result = new ArrayList<TrollJoke>();
		try {
			result = trollDao.getJokeRelative(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/truyen-cuoi/{jokeId}", method = RequestMethod.GET)
	public String jokeDetail(@PathVariable int jokeId, Model model, HttpServletRequest request) {
		String path = "";
		try{
			model.addAttribute("joke", trollDao.getTrollJokeById(jokeId));
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		}catch (Exception e) {
			e.printStackTrace();
		}
		return path + "jokeDetail";
	}

	@RequestMapping(value = "/addTroll", method = RequestMethod.POST)
	public @ResponseBody
	Response addTroll(@RequestParam(value = "url") String url, @RequestParam(value = "title") String title,
			@RequestParam(value = "source") String source, HttpServletRequest request) {
		Response result = new Response();
		try {
			TrollPhoto troll = new TrollPhoto(title, source, Constant.IMAGE, url);
			Uploader uploader = (Uploader) request.getSession().getAttribute("uploader");
			troll.setUploader(uploader);
			int id = trollDao.addTrollPhoto(troll);
			result.setData(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("Không thể upload lúc này, vui lòng thử lại lần sau");
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(value = "/photo-most-view", method = RequestMethod.GET)
	public @ResponseBody
	List<TrollPhoto> getPhotoMostView() {
		List<TrollPhoto> results = new ArrayList<TrollPhoto>();
		try {
			results = trollDao.getPhotoMostView();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	@RequestMapping(value = "/joke-most-view", method = RequestMethod.GET)
	public @ResponseBody
	List<TrollJoke> getJokeMostView() {
		List<TrollJoke> results = new ArrayList<TrollJoke>();
		try {
			results = trollDao.getJokeMostView();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	@RequestMapping(value = "/update-like", method = RequestMethod.POST)
	public @ResponseBody
	boolean updateLike(@RequestParam(value = "id") int id) {
		try {
			trollDao.updateLike(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	@RequestMapping(value = "/update-comment", method = RequestMethod.POST)
	public @ResponseBody
	boolean updateComment(@RequestParam(value = "id") int id) {
		try {
			trollDao.updateComment(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
	
	@RequestMapping(value = "/giai-thuong", method = RequestMethod.GET)
	public String giaiThuong(Model model, HttpServletRequest request) {
		String path = "";
		try {
			model.addAttribute("awardMonth", awardDao.getAward("MONTH"));
			model.addAttribute("awardWeek", awardDao.getAward("WEEK"));
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "award";
	}
	
	@RequestMapping(value = "/tieu-chi-bxh", method = RequestMethod.GET)
	public String tieuChi(Model model, HttpServletRequest request) {
		String path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		return path + "tieuChiBXH";
	}

}
